package com.hnkywl.jip.service;

import com.hnkywl.jip.dao.VoucherDao;
import com.hnkywl.jip.dao.VoucherType;
import com.hnkywl.jip.interceptor.AuthInterceptor;
import com.hnkywl.jip.utils.Freemarker2Xml;
import com.hnkywl.jip.utils.InterfaceParam;
import com.hnkywl.jip.utils.Md5;
import com.jiuqi.gmc.IVoucherGenerateSvr;
import com.jiuqi.gmc.IVoucherRevokeSvr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.jip.service
 * 类描述:    [一句话描述该类的功能]
 * 创建人:    ZHUCHG
 * 创建时间: 13-2-22 下午2:36
 * 修改人:    ZHUCHG
 * 修改时间: 13-2-22 下午2:36
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class VoucherService {
    private InterfaceParam interfaceParam;
    private VoucherDao voucherDao;
    private IVoucherGenerateSvr voucherGenerateSvr;
    private IVoucherRevokeSvr voucherRevokeSvr;

    /**
     * 生成凭证
     *
     * @param data
     * @return
     */
    public String generateVoucher(Map data) {
        String result = null;
        try {
            String xml = Freemarker2Xml.generateVoucherXml(data);
            result = voucherGenerateSvr.generateVoucher(xml);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 撤销凭证
     *
     * @param data
     * @return
     */
    public String revokeVoucher(Map data) {
        String result = null;
        try {
            String xml = Freemarker2Xml.revokeVoucherXml(data);
            result = voucherRevokeSvr.revokeVoucher(xml);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Map getMsgHeader(Map userInfo) {
        Map<String, Object> msgheader = new HashMap<String, Object>();
        msgheader.put("sourcesystemid", interfaceParam.getSourceSystemId()); //系统标识
        msgheader.put("sourcesystemname", interfaceParam.getSourceSystemName());//系统名称
        msgheader.put("userid", userInfo.get(AuthInterceptor.JIP_USERNAME_SESSION_KEY));//用户ID
        msgheader.put("username", userInfo.get(AuthInterceptor.JIP_USERNAME_SESSION_KEY));//用户名称
        //MD5 32位小写
        msgheader.put("password", Md5.md5Encrypt(userInfo.get(AuthInterceptor.JIP_PASSWORD_SESSION_KEY).toString()));//用户密码
        msgheader.put("submitdate", new Date());//调用时间
        return msgheader;
    }

    @Transactional
    public Boolean beginGenerateVoucher(Integer id, VoucherType type) {
        return voucherDao.beginGenerateVoucher(id, type);
    }

    @Transactional
    public Boolean generateVoucherSuccess(String uuid, String voucherId, String voucherNum, Integer year, Integer id, VoucherType type) {
        return voucherDao.generateVoucherSuccess(uuid, voucherId, voucherNum, year, id, type);
    }

    @Transactional
    public Boolean beginRevokeVoucher(Integer id, VoucherType type) {
        return voucherDao.beginRevokeVoucher(id, type);
    }

    @Transactional
    public Boolean revokeVoucherSuccess(Integer id, VoucherType type) {
        return voucherDao.revokeVoucherSuccess(id, type);
    }

    @Transactional
    public String restVoucher(Integer userId, Integer id, Integer type) {
        try {
            return voucherDao.restVoucher(userId, id, type);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @Autowired
    public void setInterfaceParam(InterfaceParam interfaceParam) {
        this.interfaceParam = interfaceParam;
    }

    @Autowired
    public void setVoucherDao(VoucherDao voucherDao) {
        this.voucherDao = voucherDao;
    }



    @Autowired
    public void setVoucherGenerateSvr(IVoucherGenerateSvr voucherGenerateSvr) {
        this.voucherGenerateSvr = voucherGenerateSvr;
    }

    @Autowired
    public void setVoucherRevokeSvr(IVoucherRevokeSvr voucherRevokeSvr) {
        this.voucherRevokeSvr = voucherRevokeSvr;
    }
}
